package Sort;

import java.util.Arrays;

/**
 * @Author: 吕庆龙
 * @Date: 2019/9/30 15:48
 * <p>
 * 功能描述: 插入排序
 */
public class InsertSort {

    public static void main(String[] args) {
        int[] a = {101, 34, 119, 1};
        insertSort(a);
        System.out.println("排完序后=" + Arrays.toString(a));
    }

    public static void insertSort(int[] a) {
        //{101, 34, 119, 1}
        int insertValue = 0;
        int insertIndex = 0;
        for (int i = 1; i < a.length; i++) {
            insertValue = a[i];
            insertIndex = i - 1;
            while (insertIndex >= 0 && insertValue < a[insertIndex]) {
                a[insertIndex + 1] = a[insertIndex];
                insertIndex--;
            }
            if (insertIndex != i - 1) {
                a[insertIndex + 1] = insertValue;
            }
        }
    }
}
